#include <iostream>
#include <memory>

namespace {


class Test {
public:
    int val;
    char cval1;
    char cval2;
};


void Handle() {
    std::auto_ptr<int> p(new int(3));
    std::cout << "*p=" << *p << std::endl;

    std::auto_ptr<int> p2(p);
    std::cout << "*p2=" << *p2 << std::endl;
    // std::cout << "*p=" << *p << std::endl; // p已是空地址，取值会报错
    std::cout << "p.get()=" << p.get() << std::endl;

    std::unique_ptr<int> p3(new int(3));
    std::cout << "*p3=" << *p3 << std::endl;

    // // Disable copy from lvalue.
    // unique_ptr(const unique_ptr&) = delete;
    // unique_ptr& operator=(const unique_ptr&) = delete;
    // std::unique_ptr<int> p4(p3); // 编译错误，禁止了拷贝构造函数
    std::unique_ptr<int> p4 = std::move(p3); // p3已失效
    // std::cout << "*p3=" << *p3 << std::endl; // Segmentation fault
    std::cout << "*p4=" << *p4 << std::endl;

    std::shared_ptr<int> p5(new int(3));
    std::cout << "*p5=" << *p5 << " use count: " << p5.use_count() << std::endl;

    std::shared_ptr<int> p6(p5);
    std::cout << "*p6=" << *p6 << " use count: " << p6.use_count() << std::endl;
    
    Test *t = new Test[5];
    for (int i=0; i<5; ++i) {
        t[i].val = i;
    }
}

}

void HandlePtr() {
    Handle();
}
